{block name=body}
    {* Define the function *} {function name=options_list level=0}
    {foreach $opts as $opt}
        {if $fields.listing_id neq $opt.id}
            <option value="{$opt.id}"
                    {if  $selected eq $opt.id}selected="selected"{/if}>{for $var=1 to $level}- {/for}{$opt.value}</option>
            {if count($opt.subs) > 0} {call name=options_list opts=$opt.subs level=$level+1 selected=$selected} {/if}
        {/if}
    {/foreach}
{/function}
    <script type="text/javascript" charset="utf-8" src="../includes/js/ueditor/ueditor.config.js"></script>
    <script type="text/javascript" charset="utf-8" src="../includes/js/ueditor/ueditor.all.min.js"></script>
    <!--建议手动加在语言，避免在ie下有时因为加载语言失败导致编辑器加载失败-->
    <!--这里加载的语言文件会覆盖你在配置项目里添加的语言类型，比如你在配置项目里配置的是英文，这里加载的中文，那最后就是中文-->
    <script type="text/javascript" charset="utf-8" src="../includes/js/ueditor/lang/zh-cn/zh-cn.js"></script>
    <div class="row">
        <div class="col-sm-12">
            <form class="well form-horizontal" id="newEditForm" accept-charset="UTF-8" method="post">
                <div class="row">
                    <div class="col-sm-12 edit-page-header">
                        <span class="edit-page-title">{if $editState eq "edit"}编辑{else}新建{/if} 新闻</span>
                    </div>
                </div>
                <div class="row published" >
                    <div class="alert alert-success text-center">
                        {if $new.publish eq 1}
                            <strong>已发布</strong>
                        {else}
                            <strong>草稿</strong>
                        {/if}
                    </div>
                </div>
                <div class="tab-content">
                    <!--===+++===+++===+++===+++===+++ DETAILS TAB +++===+++===+++===+++===+++====-->
                    <div class="row form" data-error="<b>详情</b>选项上发现错误。打开<b>详情</b>选项以查看特定错误通知。">
                        <div class="row form-group">
                            <label class="col-sm-3 control-label" for="newsTitleId">分类标签 *</label>
                            <div class="col-sm-5">
                                <select class="form-control" name="news_title_id" id="newsTitleTab">
                                    <option value="-1">请选择新闻分类标签</option>
                                    {foreach $tabs as $tab}
                                        <option value="{$tab.id}" {if $new.news_title_id eq $tab.id}selected{/if}>{$tab.title}</option>
                                    {/foreach}
                                </select>
                            </div>
                        </div>
                        <div class="row form-group">
                            <label class="col-sm-3 control-label" for="newTitle">标题 *</label>
                            <div class="col-sm-5">
                                <input class="form-control" type="text" value="{if $new}{$new.title}{/if}" name="title" id="newTitle"
                                       required>
                            </div>
                        </div>
                        <div class="row form-group">
                            <label class="col-sm-3 control-label" for="newDescription">摘要 *</label>
                            <div class="col-sm-5">
                                <input class="form-control" type="text" value="{if $new}{$new.content}{/if}" name="content"
                                       id="newDescription">
                            </div>
                        </div>

                        <div class="row form-group">
                            <label class="col-sm-3 control-label" for="newContent">新闻内容 *</label>
                            <div class="col-sm-5">
                                <script id="newContent" type="text/plain" data-name="article"></script>
                            </div>
                        </div>
                        <div class="row form-group">
                            <label class="col-sm-3 control-label" for="newStartDate">开始日期 *</label>
                            <div class="col-sm-5">
                                <input class="form-control dates" type="text"
                                       value="{if $new.start_time}{$new.start_time|date_format:"%Y-%m-%d"}{else}{$smarty.now|date_format:"%Y-%m-%d"}{/if}"
                                       name="start_time" id="newStartDate">
                            </div>
                        </div>
                        <div class="row form-group">
                            <label class="col-sm-3 control-label" for="newEndDate">结束日期 </label>
                            <div class="col-sm-5">
                                <input class="form-control dates" type="text"
                                       value="{if $new.stop_time}{$new.stop_time|date_format:"%Y-%m-%d"}{/if}"
                                       name="stop_time" id="newEndDate">
                            </div>
                        </div>
                        <div class="row form-group">
                            <label class="col-sm-3 control-label" for="listing_image">标题图片 *<br>
                                <small>限1张，尺寸: 480px宽 * 480px高</small>
                            </label>
                            <div class="col-sm-9" style="padding-top:1%;">
                                <div class="">
                                    <input type="hidden" value="{if $new.image}{$new.image}{/if}" id="newTitleImg" name="image">
                                    <input type="hidden" value='{if $new.article}{$new.article}{/if}' id="newArticle">
                                    <input type="file" style="display:none;" value="" id="newTitleImgUploader"/>
                                    <button class="btn btn-info" id="newTitleImgChoose"><span
                                                class="glyphicon glyphicon-folder-open"></span> 选择图片
                                    </button>
                                    <button class="btn btn-danger" id="newTitleImgClear"><span
                                                class="glyphicon glyphicon-remove"></span> 取消选择
                                    </button>
                                </div>
                                <div class="col-sm-5 img-thumbnail" id="newTitleImgPreview" style="{if !$new.image}display:none;{/if}margin-top:1%;">
                                    <img src="{if $new.image}{$new.image}{/if}" class="img-responsive" />
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="row form-group form-bottom-btns" data-state="{$editState}" {if $newid} data-newid="{$newid}" {/if} id="newEditBtnGroup">
                    <a class="btn btn-warning pull-right top-btn" id="saveBtn"><span class="glyphicon glyphicon-floppy-saved"></span> 保存 &amp; 不发布</a>
                    <a class="btn btn-primary pull-right top-btn drafts" id="saveAndPubBtn"><span class="glyphicon glyphicon-thumbs-up"></span> 保存 &amp; 发布</a>
                </div>
                <input type="hidden" name="formToken" value="{$token}"/>
            </form>
        </div>
    </div>
    <script type="text/javascript">
        $(document).ready(function () {
            //开启富文本控件
            var newContent = UE.getEditor('newContent');

            newContent.on('ready',function(){
                var newArticle = $("#newArticle").val();
                if(newArticle!='')
                    UE.getEditor('newContent').setContent(newArticle,false);
            });

            //开启时间控件
            $("#newStartDate").datepicker({
                changeMonth: true,
                changeYear: true,
                dateFormat: "yy-mm-dd",
                minDate:$("#newStartDate").val(),
                onSelect: function (selectedDate) {
                    $("#id_news_start_date").val(convert_to_mysql_date_format(selectedDate));
                    $("#newEndDate").datepicker("option", "minDate", selectedDate);
                }
            });
            $("#newEndDate").datepicker({
                changeMonth: true,
                changeYear: true,
                dateFormat: "yy-mm-dd",
                minDate:$("#newStartDate").val(),
                onSelect: function (selectedDate) {
                    $("#id_news_end_date").val(convert_to_mysql_date_format(selectedDate));
                    $("#newStartDate").datepicker("option", "maxDate", selectedDate);
                }
            });

            //标题图片选择
            $("#newTitleImgChoose").on('click', function (e) {
                e.preventDefault();
                $("#newTitleImgUploader").click();
            });

            //标题图片自动上传
            $("#newTitleImgUploader").on('change', function () {
                var fd = new FormData();
                fd.append('upfile', $(this)[0].files[0]);
                $.ajax({
                    url: '../includes/js/ueditor/php/controller.php?action=uploadimage',
                    data: fd,
                    type: 'POST',
                    cache: 'false',
                    processData: false,
                    contentType: false,
                    dataType: 'json',
                    success: function (data) {
                        console.log(data);
                        if (data.state == 'SUCCESS') {
                            $("#newTitleImgPreview img").attr('src', data.url);
                            $("#newTitleImgPreview").show();
                            $("#newTitleImg").val(data.url);
                        }
                    }
                });
            });

            //标题图片取消选择
            $("#newTitleImgClear").on('click', function (e) {
                e.preventDefault();
                $("#newTitleImg").val('');
                $("#newTitleImgPreview").hide();
            });

            //保存按钮
            $("#saveBtn").on('click',function(e){
                e.preventDefault();
                if(checkForm()){
                    var fd=new FormData($("#newEditForm")[0]);
                    var editState=$(this).parent().attr('data-state');
                    if(editState=='edit'){
                        fd.append('type','edit');
                        fd.append('id',$(this).parent().attr('data-newid'));
                    } else {
                        fd.append('type','add');
                    }
                    fd.append('publish',0);
                    $.ajax({
                        url: '../includes/processes/processes-news-controller.php',
                        data: fd,
                        type: 'POST',
                        cache: 'false',
                        processData: false,
                        contentType: false,
                        dataType: 'json',
                        success: function (data) {
                            console.log(data);
                            if(data.type==1){
                                commonAlertBox(data.content,'自动跳转列表页',3);
                                setTimeout(function(){
                                    window.location.href="/admin/general/newlist";
                                },3000);
                            }else{
                                commonAlertBox(data.content);
                            }
                        }
                    });
                }
            });

            //保存并发布
            $("#saveAndPubBtn").on('click',function(e){
                e.preventDefault();
                if(checkForm()){
                    var fd=new FormData($("#newEditForm")[0]);
                    var editState=$(this).parent().attr('data-state');
                    if(editState=='edit'){
                        fd.append('type','edit');
                        fd.append('id',$(this).parent().attr('data-newid'));
                    } else {
                        fd.append('type','add');
                    }
                    fd.append('publish',1);
                    $.ajax({
                        url: '../includes/processes/processes-news-controller.php',
                        data: fd,
                        type: 'POST',
                        cache: 'false',
                        processData: false,
                        contentType: false,
                        dataType: 'json',
                        success: function (data) {
                            console.log(data);
                            if(data.type==1){
                                commonAlertBox(data.content,'自动跳转列表页',3);
                                setTimeout(function(){
                                    window.location.href="/admin/general/newlist";
                                },3000);
                            }else{
                                commonAlertBox(data.content);
                            }
                        }
                    });
                }
            });
            function checkForm(){
                var checkState=1;//1 通过   0不通过
                var newsTitleTab=$("#newsTitleTab option:selected").val();
                var newTitle=$("#newTitle").val();
                var newDescription=$("#newDescription").val();
                var article=UE.getEditor('newContent').getContent();
                var startTime=$("#newStartDate").val();
                var newTitleImg=$("#newTitleImg").val();
                if(newsTitleTab==-1){
                    $("#newsTitleTab").parents(".form-group").addClass("has-error");
                    checkState=0;
                }else{
                    $("#newsTitleTab").parents(".form-group").removeClass("has-error").addClass("has-success");
                }
                if(!newTitle){
                    $("#newTitle").parents(".form-group").addClass("has-error");
                    checkState=0;
                }else{
                    $("#newTitle").parents(".form-group").removeClass("has-error").addClass("has-success");
                }
                if(!newDescription){
                    $("#newDescription").parents(".form-group").addClass("has-error");
                    checkState=0;
                }else{
                    $("#newDescription").parents(".form-group").removeClass("has-error").addClass("has-success");
                }
                if(!startTime){
                    $("#newStartDate").parents(".form-group").addClass("has-error");
                    checkState=0;
                }else{
                    $("#newStartDate").parents(".form-group").removeClass("has-error").addClass("has-success");
                }
                if(!newTitleImg){
                    $("#newTitleImg").parents(".form-group").addClass("has-error");
                    checkState=0;
                }else{
                    $("#newTitleImg").parents(".form-group").removeClass("has-error").addClass("has-success");
                }
                if(!article){
                    $("#newContent").parents(".form-group").addClass("has-error");
                    checkState=0;
                }else{
                    $("#newContent").parents(".form-group").removeClass("has-error").addClass("has-success");
                }
                $("#newEndDate").parents(".form-group").addClass("has-success");
                if(checkState==1){
                    return true;
                }else{
                    commonAlertBox("有必填项为空，请检查后再提交");
                    return false;
                }
            }
        });
    </script>
{/block}
